﻿Imports Framework.Data

Public Class Shares
    Shared Sub New()
        Framework.Security.KeyMaster.RegisterDissolver(Of Entities.Share)(AddressOf SecurityGroupDissolver)
    End Sub
    Private Shared Function SecurityGroupDissolver(ByVal share As Entities.Share) As Generic.HashSet(Of Integer)
        If share Is Nothing Then
            Return Nothing
        Else
            Return share.SecurityGroupIds
        End If
    End Function


#Region " Events "
    Public Shared Event Deleted(ByVal share As Entities.Share)
    Public Shared Event DeletedGUI(ByVal share As Entities.Share)
    Friend Shared Sub OnDeleted(ByVal share As Entities.Share)
        RaiseEvent Deleted(share)
        Framework.Threading.Invoke(AddressOf OnDeleted_Callback, share)
    End Sub
    Private Shared Sub OnDeleted_Callback(ByVal share As Entities.Share)
        RaiseEvent DeletedGUI(share)
    End Sub

    Public Shared Event Updated(ByVal share As Entities.Share)
    Public Shared Event UpdatedGUI(ByVal share As Entities.Share)
    Friend Shared Sub OnUpdated(ByVal share As Entities.Share)
        RaiseEvent Updated(share)
        Framework.Threading.Invoke(AddressOf OnUpdated_Callback, share)
    End Sub
    Private Shared Sub OnUpdated_Callback(ByVal share As Entities.Share)
        RaiseEvent UpdatedGUI(share)
    End Sub
#End Region

    Public Shared Function GetAll() As Generic.List(Of Entities.Share)
        Return DataStore.SharesEx.GetAll
    End Function
    Public Shared Function GetAll(ByVal keyMaster As Framework.Security.KeyMaster) As Generic.List(Of Entities.Share)
        Dim aShares As Generic.List(Of Entities.Share) = GetAll()
        For i As Integer = aShares.Count - 1 To 0 Step -1
            Dim clsShare As Entities.Share = aShares(i)
            If Not keyMaster.IsGranted(clsShare) Then aShares.RemoveAt(i)
        Next
        Return aShares
    End Function
    Public Shared Function GetById(ByVal id As Integer) As Entities.Share
        Return DataStore.SharesEx.GetById(id)
    End Function
    Public Shared Sub Delete(ByVal id As Integer)
        Using t As New TransactionScope
            Dim clsShare As Entities.Share = GetById(id)

            SharedItems.DeleteAllByShareId(clsShare.Id, t)
            DataStore.SharesEx.Delete(id, t)
            t.RegisterOnCompleteCallback(AddressOf OnDeleteTransactionComplete, "DeleteShare", id, clsShare)
            t.Complete()
        End Using
    End Sub
    Private Shared Sub OnDeleteTransactionComplete(ByVal arg1 As String, ByVal arg2 As Integer, ByVal arg3 As Object)
        OnDeleted(CType(arg3, Entities.Share))
    End Sub
    Public Shared Sub Save(ByVal share As Entities.Share)
        DataStore.SharesEx.Save(share)
        OnUpdated(share)
    End Sub

    Public Shared Function GetAllAsInfos() As Generic.List(Of Entities.Infos.ShareInfo)
        Return DataStore.SharesEx.GetAllAsInfos()
    End Function

End Class